package servlets;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

public class ServletUtils {
	public static String getMenu(String titel) {
		return "<h1>"
				+ titel
				+ "</h1>"
				+ "<a href=\"../Sessions/Startseite\">Startseite</a><br />"
				+ "<a href=\"../Sessions/Angebote\">Angebote</a><br />"
				+ "<a href=\"../Sessions/Sonderangebote\">Sonderangebote</a><br /><br />"
				+ "<a href=\"../Sessions/Logout\">Logout</a><br /><br />";
	}

	public static Integer getSessionCounter(HttpSession session) {
		Integer counter = (Integer) session.getAttribute("zaehler");
		if (counter == null) {
			counter = new Integer(0);
		} else {
			counter = new Integer(counter.intValue() + 1);
		}
		session.setAttribute("zaehler", counter);
		return counter;
	}

	public static ArrayList<String> getVisitedPagenames(HttpSession session,
			String visitedPagename) {
		@SuppressWarnings("unchecked")
		ArrayList<String> al = (ArrayList<String>) session
				.getAttribute("visitedPagename");
		if (al == null) {
			al = new ArrayList<String>();
		} else {
			al.add(visitedPagename);
		}
		session.setAttribute("visitedPagename", al);
		return al;
	}

	public static String getHistory(HttpSession session, String visitedPagename) {
		String output = "";

		Integer counter = ServletUtils.getSessionCounter(session);
		ArrayList<String> al = ServletUtils.getVisitedPagenames(session,
				visitedPagename);
		if (al.size() == 0)
			return "Erster Besuch";
		if (al.size() == counter) {
			for (int i = 0; i < al.size(); i++) {
				output += (i + 1) + ".: " + al.get(i) + "<br />";
			}
			return output;
		} else
			return "Fehler";
	}

}
